package com.osi.project.common.business;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author limingcheng
 * @description SwaggerConfig
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Profile({"dev", "test"})
public class SwaggerConfig2 {

    @Bean
    public Docket sysauthApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("redis标记")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.osi.project.redis.controller"))
                .paths(PathSelectors.any())
                .build().pathMapping("/").globalOperationParameters(setHeaderParam());
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档")
                .version("1.0.0")
                .termsOfServiceUrl("http://xxx/doc.html")
                .build();
    }

    private List<Parameter> setHeaderParam() {

        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        Parameter authorization = parameterBuilder.name("Authorization").description("头部系统参数，登录token")//name表示名称，description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false)
                .defaultValue(
                        "eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE1ODAzOTA1MzMsInN1YiI6IntcImNyZWF0ZVRpbWVcIjpcIjIwMjAtMDEtMzAgMjA6MjI6MTNcIixcImRlcHRJZFwiOlwiMTAwMDBcIixcInJvbGVJZHNcIjpcIjEwMDAwLDEwMDAxXCIsXCJ1c2VySWRcIjpcIjEwMDAwMDBcIixcImFwcEtleVwiOlwid3hNaW5pQXBwRXZjc1wiLFwidXNlck5hbWVcIjpcImFkbWluXCIsXCJ1c2VyVHlwZVwiOjEyfSJ9.WKncbtZ4y1o8MYpVJRWNkRRrfErs3YQk557dOGE71E-lb8_23vhXm6wYdHx1clIMwNJsrchw2ttOANlInHScNw")
                .build();//required表示是否必填，defaultvalue表示默认值

        Parameter headParams = parameterBuilder.name("HeaderParams")
                .description("头部系统参数，json串由appKey,appVersion,apiVersion,timestamp")//name表示名称，description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(true)
                .defaultValue(
                        "{\"apiVersion\":\"1.0.0\",\"appKey\":\"wxMiniAppEvcs\",\"appVersion\":\"1.2.2\",\"timestamp\":\"1580264617867\"}")
                .build();//required表示是否必填，defaultvalue表示默认值

        Parameter sign = parameterBuilder.name("Sign")
                .description("头部系统参数，签名数据，Authorization + HeaderParams + Body + secret拼接，md5后转大写")//name表示名称，description表示描述
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(true).defaultValue("E271E4AEF487D79319F4C215B6A61A02").build();//required表示是否必填，defaultvalue表示默认值

        pars.add(authorization);
        pars.add(headParams);
        pars.add(sign);
        return pars;
    }

}
